package com.note.chh.notebook.UI.notes;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.photoview.PhotoView;
import com.luck.picture.lib.widget.PreviewViewPager;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Uitls.UserUtil;
import com.note.chh.notebook.Widget.ScaleView;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

//查看图片
public class CheckPictureActivity extends BaseActivity {

    @BindView(R.id.mViewPager)
    PreviewViewPager mViewPager;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    private List<String> paths = null;
    private int currentNum = 0;
    private ArrayList<View> views;

    @Override
    protected int bindLayout() {
        return R.layout.activity_check_picture;
    }

    @Override
    public void initTopBar() {
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.app_color_blue));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initView() {
        Bundle bundle = getIntent().getExtras();
        paths = bundle.getStringArrayList("path");
        Log.d("chh", "paths.size = " + paths.size());
        currentNum = bundle.getInt("currentNum");
        if (bundle.getBoolean("isAdd")) {
            paths.remove(paths.size() - 1);
        }
        for (int i = 0; i < paths.size(); i++) {
            PhotoView scaleView = new PhotoView(CheckPictureActivity.this);
            UserUtil.dLog("--- path = " + paths.get(i).split("\\|")[1]);
            Glide.with(CheckPictureActivity.this).load(paths.get(i).split("\\|")[1]).into(scaleView);
            if (views == null) {
                views = new ArrayList<>();
            }
            views.add(scaleView);
        }
        topbar.setTitle(currentNum+1+" / "+views.size());
        mViewPager.setAdapter(new myPagerAdapter());
        mViewPager.setCurrentItem(currentNum);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                topbar.setTitle(position+1+" / "+views.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.slide_still, R.anim.alpha_out);
    }


    private class myPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//          if (object instanceof ScaleView) {
//              ScaleView scaleView = (ScaleView) object;
//              container.removeView(scaleView);
//          }
            container.removeView(views.get(position));
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(views.get(position), 0);
            return views.get(position);
        }
    }

}
